package com.aipsi.cws.supplier.serviceImpl;

import com.aipsi.cws.common.customAssert.Assert;
import com.aipsi.cws.supplier.domain.Supplier;
import com.aipsi.cws.supplier.dvo.CommonVO;
import com.aipsi.cws.supplier.mapper.SupplierMapper;
import com.aipsi.cws.supplier.service.ISupplierService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.aipsi.cws.supplier.constants.ControllerConstants.DEBT_FILED_NAME;
import static com.aipsi.cws.supplier.util.CommonUtil.getPage;
import static com.aipsi.cws.supplier.util.CommonUtil.getQueryWrapperOfOverDraftAmount;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大忽悠
 * @since 2022年03月16日
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements ISupplierService {
    @Override
    public Supplier getOne(String supplierName) {
        Supplier one = getOne(new QueryWrapper<Supplier>().eq("supplier_name", supplierName));
        Assert.notNull(one, "记录不存在");
        return one;
    }

    @Override
    public List<Supplier> getSuppliers(CommonVO commonVO) {
        Page<Supplier> supplierPage = page(getPage(commonVO),getQueryWrapperOfOverDraftAmount(commonVO).orderByDesc(DEBT_FILED_NAME));
        return supplierPage.getRecords();
    }

    @Override
    public List<Supplier> getSuppliersVagued(String supplierNameVagued, String legalPersonVagued, String phoneNumberVagued, CommonVO commonVO) {
        Page<Supplier> supplierPage = page(getPage(commonVO), choseWhichToMatch(supplierNameVagued, legalPersonVagued, phoneNumberVagued, commonVO)
                .orderByDesc(DEBT_FILED_NAME));
        return supplierPage.getRecords();
    }

    private QueryWrapper<Supplier> choseWhichToMatch(String supplierNameVagued, String legalPersonVagued, String phoneNumberVagued, CommonVO commonVO) {
        QueryWrapper<Supplier> queryWrapper = getQueryWrapperOfOverDraftAmount(commonVO);
                if(supplierNameVagued!=null){
                    queryWrapper.like("supplier_name", supplierNameVagued).or();
                }
                if(legalPersonVagued!=null){
                    queryWrapper.like("legal_person", legalPersonVagued).or();
                }
                if(phoneNumberVagued!=null){
                    queryWrapper.like("mobile_number", phoneNumberVagued).or().like("phone_number", phoneNumberVagued);
                }
        return queryWrapper;
    }

    @Override
    public Boolean remove(String supplierName) {
        return remove(new QueryWrapper<Supplier>().eq("supplier_name", supplierName));
    }

    @Override
    public Boolean putSupplier(Supplier supplier) {
        Supplier one =getOne(new QueryWrapper<Supplier>().eq("supplier_name", supplier.getSupplierName()));
        Assert.notNull(one, "记录不存在");
        return updateById(supplier);
    }

    /**
     * 插入一条记录（选择字段，策略插入）
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public boolean save(Supplier entity) {
        if(entity.getSupplierName()==null || entity.getSupplierName().isEmpty() || entity.getAccountId()==null){
           throw new IllegalArgumentException("supplierName或者accountId不能为空");
        }
        return super.save(entity);
    }
}
